﻿using System.Web.Security;

namespace TeamsPortal
{
    public interface IMembershipService
    {
        string ApplicationName { get; set; }
        bool EnablePasswordReset { get; }
        bool EnablePasswordRetrieval { get; }
        string HashAlgorithmType { get; }
        int MaxInvalidPasswordAttempts { get; }
        int MinRequiredNonAlphanumericCharacters { get; }
        int MinRequiredPasswordLength { get; }
        int PasswordAttemptWindow { get; }
        string PasswordStrengthRegularExpression { get; }
        MembershipProvider Provider { get; }
        MembershipProviderCollection Providers { get; }
        bool RequiresQuestionAndAnswer { get; }
        int UserIsOnlineTimeWindow { get; }
        event MembershipValidatePasswordEventHandler ValidatingPassword;
        MembershipUser CreateUser(string username, string password);
        MembershipUser CreateUser(string username, string password, string email);
        MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, out MembershipCreateStatus status);
        MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status);
        bool DeleteUser(string username);
        bool DeleteUser(string username, bool deleteAllRelatedData);
        MembershipUserCollection FindUsersByEmail(string emailToMatch);
        MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords);
        MembershipUserCollection FindUsersByName(string usernameToMatch);
        MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords);
        string GeneratePassword(int length, int numberOfNonAlphanumericCharacters);
        MembershipUserCollection GetAllUsers();
        MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords);
        int GetNumberOfUsersOnline();
        MembershipUser GetUser();
        MembershipUser GetUser(bool userIsOnline);
        MembershipUser GetUser(object providerUserKey);
        MembershipUser GetUser(string username);
        MembershipUser GetUser(object providerUserKey, bool userIsOnline);
        MembershipUser GetUser(string username, bool userIsOnline);
        string GetUserNameByEmail(string emailToMatch);
        void UpdateUser(MembershipUser user);
        bool ValidateUser(string username, string password);
        bool ChangePassword(MembershipUser user, string oldPassword);
    }

    public class MembershipService : IMembershipService
    {
        public string ApplicationName
        {
            get { return Membership.ApplicationName; }
            set { Membership.ApplicationName = value; }
        }

        public bool EnablePasswordReset
        {
            get { return Membership.EnablePasswordReset; }
        }

        public bool EnablePasswordRetrieval
        {
            get { return Membership.EnablePasswordRetrieval; }
        }

        public string HashAlgorithmType
        {
            get { return Membership.HashAlgorithmType; }
        }

        public int MaxInvalidPasswordAttempts
        {
            get { return Membership.MaxInvalidPasswordAttempts; }
        }

        public int MinRequiredNonAlphanumericCharacters
        {
            get { return Membership.MinRequiredNonAlphanumericCharacters; }
        }

        public int MinRequiredPasswordLength
        {
            get { return Membership.MinRequiredPasswordLength; }
        }

        public int PasswordAttemptWindow
        {
            get { return Membership.PasswordAttemptWindow; }
        }

        public string PasswordStrengthRegularExpression
        {
            get { return Membership.PasswordStrengthRegularExpression; }
        }

        public MembershipProvider Provider
        {
            get { return Membership.Provider; }
        }

        public MembershipProviderCollection Providers
        {
            get { return Membership.Providers; }
        }

        public bool RequiresQuestionAndAnswer
        {
            get { return Membership.RequiresQuestionAndAnswer; }
        }

        public int UserIsOnlineTimeWindow
        {
            get { return Membership.UserIsOnlineTimeWindow; }
        }

        public event MembershipValidatePasswordEventHandler ValidatingPassword;

        public MembershipUser CreateUser(string username, string password)
        {
            return Membership.CreateUser(username, password);
        }

        public MembershipUser CreateUser(string username, string password, string email)
        {
            return Membership.CreateUser(username, password, email);
        }

        public MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, out MembershipCreateStatus status)
        {
            return Membership.CreateUser(username, password, email, passwordQuestion, passwordAnswer, isApproved, out status);
        }

        public MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            return Membership.CreateUser(username, password, email, passwordQuestion, passwordAnswer, isApproved, providerUserKey, out status);
        }

        public bool DeleteUser(string username)
        {
            return Membership.DeleteUser(username);
        }

        public bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            return Membership.DeleteUser(username, deleteAllRelatedData);
        }

        public MembershipUserCollection FindUsersByEmail(string emailToMatch)
        {
            return Membership.FindUsersByEmail(emailToMatch);
        }

        public MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            return Membership.FindUsersByEmail(emailToMatch, pageIndex, pageSize, out totalRecords);
        }

        public MembershipUserCollection FindUsersByName(string usernameToMatch)
        {
            return Membership.FindUsersByName(usernameToMatch);
        }

        public MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            return Membership.FindUsersByName(usernameToMatch, pageIndex, pageSize, out totalRecords);
        }

        public string GeneratePassword(int length, int numberOfNonAlphanumericCharacters)
        {
            return Membership.GeneratePassword(length, numberOfNonAlphanumericCharacters);
        }

        public MembershipUserCollection GetAllUsers()
        {
            return Membership.GetAllUsers();
        }

        public MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
        {
            return Membership.GetAllUsers(pageIndex, pageSize, out totalRecords);
        }

        public int GetNumberOfUsersOnline()
        {
            return Membership.GetNumberOfUsersOnline();
        }

        public MembershipUser GetUser()
        {
            return Membership.GetUser();
        }

        public MembershipUser GetUser(bool userIsOnline)
        {
            return Membership.GetUser(userIsOnline);
        }

        public MembershipUser GetUser(object providerUserKey)
        {
            return Membership.GetUser(providerUserKey);
        }

        public MembershipUser GetUser(string username)
        {
            return Membership.GetUser(username);
        }

        public MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            return Membership.GetUser(providerUserKey, userIsOnline);
        }

        public MembershipUser GetUser(string username, bool userIsOnline)
        {
            return Membership.GetUser(username, userIsOnline);
        }

        public string GetUserNameByEmail(string emailToMatch)
        {
            return Membership.GetUserNameByEmail(emailToMatch);
        }

        public void UpdateUser(MembershipUser user)
        {
            Membership.UpdateUser(user);
        }

        public bool ValidateUser(string username, string password)
        {
            return Membership.ValidateUser(username, password);
        }

        /// <summary>
        /// Changes the password of a user without asking the old password.
        /// </summary>
        public bool ChangePassword(MembershipUser user, string newPassword)
        {
            //var currentUser = Membership.GetUser(userName, true);
            string oldPassword = user.ResetPassword();
            return user.ChangePassword(oldPassword, newPassword);
        }
    }
}
